var lottery;

var Results = function() {
    var a = {
        dx:{
            D:"大",
            X:"小",
            T:"和",
            N:"通吃",
            LOSE:"输"
        },
        xwdx:{
            D:"大",
            X:"小",
            T:"和",
            N:"通吃"
        },
        ds:{
            D:"单",
            S:"双",
            T:"和",
            LOSE:"输"
        },
        hds:{
            D:"单",
            S:"双",
            T:"和"
        },
        lh:{
            L:"龙",
            H:"虎",
            T:"和"
        },
        sb:{
            R:"红",
            G:"绿",
            B:"蓝"
        },
        fw:{
            "0":"东",
            "1":"南",
            "2":"西",
            "3":"北"
        },
        zfb:{
            "0":"中",
            "1":"发",
            "2":"白"
        },
        wx:{
            "0":"金",
            "1":"木",
            "2":"水",
            "3":"火",
            "4":"土"
        },
        flsx:{
            F:"福",
            L:"禄",
            S:"寿",
            X:"喜",
            T:"和"
        },
        qh:{
            Q:"前",
            H:"後",
            T:"和"
        }
    };
    return {
        inited:false,
        tabKeys:null,
        maxCols:25,
        index:null,
        init:function(f, e, b, h, d, g) {
            var c = $("#resultPanel");
            if (!c.length) {
                c = $('<div id="resultPanel">').appendTo($("#main"));
            }
            if (e != null && b != null) {
                this.minBall = e;
                this.maxBall = b;
                this.index = d || 1;
                this.ballTotal = !!g;
                this.initBallPanel(c, h);
            }
            this.tabs = f;
            this.initTabs(c);
            this.inited = true;
        },
        initBallPanel:function(b, e) {
            var j = $("<tr>").appendTo($("<table>").addClass("tabTitle").appendTo(b));
            var d = this;
            for (var h = 0; h < e.length; h++) {
                var q = e[h];
                var m = $("<a>").data("idx", h + 1).text(q).attr("href", "javascript:void(0)");
                m.appendTo($("<th>").appendTo(j));
                m.click(function() {
                    var i = $(this);
                    i.parent().parent().find("th.selected").removeClass("selected");
                    i.parent().addClass("selected");
                    d.changeBall(i.text(), i.data("idx"));
                });
            }
            j.find("a").eq(this.index - 1).click();
            var o = $("<table>").addClass("ballTable").appendTo(b);
            if (lottery == "XYNC") {
                var c = $("<tr>").addClass("head").appendTo(o);
                c.append($("<th>").text("类型").addClass("title"));
                var g = new Array("西瓜", "椰子", "榴莲", "柚子", "菠萝", "葡萄", "荔枝", "樱桃", "草莓", "番茄", "梨子", "苹果", "桃子", "柑橘", "冬瓜", "萝卜", "南瓜", "茄子", "家犬", "奶牛");
                for (var h = 0; h < g.length; h++) {
                    c.append($("<th>").text(g[h]));
                }
            }
            var l = $("<tr>").addClass("head").appendTo(o);
            var k = $("<tr>").appendTo(o);
            var f = false;
            if (this.ballTotal) {
                f = $("<tr>").appendTo(o);
                l.append($("<th>").text("号码").addClass("title"));
                k.append($("<th>").text("出 球 率"));
                f.append($("<th>").text("无出期数"));
            }
            for (var h = this.minBall; h <= this.maxBall; h++) {
                var n = h;
                if (this.maxBall > 10 && h < 10) {
                    n = "0" + h;
                }
                l.append($("<th>").addClass("b" + h).text(n));
                k.append($("<td>").addClass("b" + h).text("0"));
                if (f) {
                    f.append($("<td>").addClass("b" + h).text("0"));
                }
            }
            this.ballTable = o;
            this.ballLine = k;
            this.ballTotalLine = f;
        },
        initTabs:function(b) {
            var i = this;
            var f = this.tabs;
            var e = $("<tr>").appendTo($("<table>").addClass("tabTitle").appendTo(b));
            this.tabTitles = e;
            var h = [];
            var l = {};
            for (var d in f) {
                var c = $("<th>").appendTo(e);
                var j = f[d];
                var g = $("<a>").data("option", j).text(d).attr("href", "javascript:void(0)");
                if (j.length > 2) {
                    l[j[0] + "_" + j[2][0]] = $("<span>").appendTo(c);
                    g.appendTo(c);
                    l[j[0] + "_" + j[2][1]] = $("<span>").appendTo(c);
                    h.push(j[0]);
                } else {
                    g.appendTo(c);
                }
                if (d == "") {
                    g.addClass("ball");
                    if (this.ballText) {
                        g.text(this.ballText);
                    }
                }
                g.click(function() {
                    var k = $(this);
                    k.parent().parent().find("th.selected").removeClass("selected");
                    k.parent().addClass("selected");
                    i.changeTab(k.data("option"));
                });
            }
            this.totalKeys = h;
            this.totalItems = l;
            this.tabPanel = $("<tr>").appendTo($("<table>").addClass("tabContents").appendTo(b));
            this.changeIndex(0);
        },
        changeBall:function(c, b) {
			
            this.ballText = c;
            if (this.inited) {
                this.tabTitles.find("a.ball").text(c);
                this.index = b;
                this.showResults();
            }
        },
        changeIndex:function(b) {
            this.tabTitles.find("a:eq(" + b + ")").click();
        },
        changeTab:function(b) {
            this.tabKeys = b;
            this.showResults();
        },
        getKey:function(b) {
            if (this.index != null) {
                return b.format(this.index);
            }
            return b;
        },
        showBalls:function(m) {
            if (!this.ballLine) {
                return;
            }
            var p = this.getKey("B{0}");//alert(p)
            var b = {};//alert(m[0].detail['B1'])
			var count=m[0].detail['count'];
            for (var g = 0; g < m.length; g++) {
				
                var k = Number(m[g].detail[p]);
				
                var l = b[k];
                 if (!l) {
                    l = 0;
                }
                l += 1;
                b[k] = l;
		
				
				
            }
			for(var c=0;c<count;c++){
				if(b[c]===undefined){
					b[c]=0;
				}
			}
			
		
            for (var k in b) {
				
                this.ballLine.children(".b" + k).text(b[k]);
            }
			
            if (this.ballTotal) {
                var o = {};
                var l = this.maxBall - this.minBall + 1;
                var d;
                for (var g = 0; g < m.length; g++) {
                    var h = m[g].result.split(",");
                    for (var f = 0; f < h.length; f++) {
                        var k = Number(h[f]);
                        if (o[k] !== undefined) {
                            continue;
                        }
                        o[k] = g;
                        l -= 1;
                        if (l <= 0) {
                            d = g;
                            break;
                        }
                    }
                }
                if (l > 0) {
                    d = m.length;
                }
                this.ballTotalLine.children(".max").removeClass("max");
                for (var g = this.minBall; g <= this.maxBall; g++) {
                    var n = o[g];
                    if (n === undefined) {
                        n = d;
                    }
                    var e = this.ballTotalLine.children(".b" + g).text(n);
                    if (n == d) {
                        e.addClass("max");
                    }
                }
            }
        },
        showTotal:function(f) {
            var n = this.totalKeys;
            var g = this.totalItems;
            if (n.length == 0) {
                return;
            }
            var h = {};
            for (var e = 0; e < f.length; e++) {
                var b = f[e];
                for (var d = 0; d < n.length; d++) {
                    var m = n[d];
                    var l = b.detail[this.getKey(m)];
                    if (l[0] == "*") {
                        continue;
                    }
                    m = m + "_" + l;
                    h[m] = (h[m] || 0) + 1;
                }
            }
            for (var c in g) {
                g[c].text(h[c] || 0);
            }
        },
        showTabs:function(n) {
            var c = this.tabPanel;
            c.empty();
            var k = [];
            var o =null;
            var m = 0;//alert(this.tabKeys[0])
            var p = this.getKey(this.tabKeys[0]);
            for (var g = 0; g < n.length; g++) {
                var b = n[g];
                var s = b.detail[p];
                if (!s) {
                    return;
                }
                if (s[0] == "*") {
                    s = "T";
                }
		
			
                if (String(s)==String(o)) {
                    m++;
                    continue;
                } else {//alert(13)
                    if (o && m) {
                        k.push([ o, m ]);
                        if (k.length >= this.maxCols) {
                            o =null;
                            break;
                        }
                    }
					 m = 1;
                }
                o =s;
             
            }
            if (o) {
                k.push([ o, m ]);
            }
            k.reverse();
            var d = this.maxCols - k.length;
            for (var g = 0; g < d; g++) {
                c.append($("<td>").html("&nbsp;"));
            }
           // var e = a[this.tabKeys[1]];alert(a['dx']['D'])
            for (var g = 0; g < k.length; g++) {
                var s = k[g];
               // var q = e ? e[s[0]] :s[0];
                //var q = e ? a[this.tabKeys[1]][s[0]] :s[0];
                var q = s[0];
                var h = q;
                for (var f = 0, l = s[1] - 1; f < l; f++) {
                    h += "<br/>" + q;
                }
                c.append($("<td>").html(h));
            }
            c.children(":even").addClass("even");
        },
        load:function() {
            if (!this.inited) {
                return;
            }
            var b = this;
            LIBS.ajax({
                url:"/index.php/Dayresult/index",
                data:{
                    lottery:lottery
                },
                cache:false,
                success:function(c) {
                    b.showResults(c);
                }
            });
        },
        showResults:function(g) {
            if (!this.inited) {
                return;
            }
            if (!g) {
                g = this.results;
            } else {
                for (var c = 0; c < g.length; c++) {
                    var e = g[c];
                    var d = e.detail;
                    if (d && !$.isPlainObject(d)) {
                        var f = d.split(";");
                        d = {};
                        for (var b = 0; b < f.length; b++) {
                            var k = f[b];
                            if (!k) {
                                continue;
                            }
                            var h = k.split("=");
                            d[h[0]] = h[1].split(",")[0];
                        }
                        e.detail = d;
                    }
                }
            }
            if (!g) {
                return;
            }
			
            this.showBalls(g);
            this.showTotal(g);
            if (typeof window.IS_MOBILE == "undefined") {
                this.showTabs(g);
            }
            this.results = g;
        }
    };
}();